﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using BT_DoHoa_Nhom13.GraphicLib;
using System.Windows.Forms;

namespace BT_DoHoa_Nhom13.Lib
{
    public class GDIPlusGraphic : MyGraphic
    {
        Graphics graphics;

        static MyGraphic instance;
        public static MyGraphic GetInstance()
        {
            if (instance == null)
            {
                instance = new GDIPlusGraphic();
            }

            return instance;
        }

        public override void SetGraphicInfo(Graphics graphic, string path)
        {
            graphics = graphic;
        }


        private GDIPlusGraphic()
        {
            name = "GDI+ Graphic";
        }

        public override void DrawLine(int x1, int y1, int x2, int y2)
        {
            graphics.DrawLine(Pens.Black,x1, y1, x2, y2);
        }

        public override void DrawEllipse(int x1, int y1, int width, int height, Boolean isFill)
        {
            if (isFill)
            {
                graphics.FillEllipse(Brushes.Black, x1, y1, width, height);
            }
            else
            {
                graphics.FillEllipse(Brushes.White, x1, y1, width, height);
                graphics.DrawEllipse(Pens.Black, x1, y1, width, height);
            }

        }

        public override void DrawRectangle(int x1, int y1, int width, int height, bool isFill)
        {
            if (isFill)
            {
                graphics.FillRectangle(Brushes.Black, x1, y1, width, height);
            }
            else
            {
                graphics.FillRectangle(Brushes.White, x1, y1, width, height);
                graphics.DrawRectangle(Pens.Black, x1, y1, width, height);
            }
        }

        public override void DrawString(int x1, int y1, int width, int height, string content)
        {
             StringFormat stringFormat = new StringFormat();
             stringFormat.Alignment = StringAlignment.Center;
             stringFormat.LineAlignment = StringAlignment.Center;
             graphics.DrawString(content, 
                                new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular), 
                                Brushes.Black, 
                                new Rectangle(x1, y1, width, height),stringFormat);
        }

        public override void DrawArc(int x, int y, int width, int height, float angleStart, float angleSweep)
        {
            if(width>10 && height > 10)
            {
                graphics.DrawArc(Pens.Black, new Rectangle(x, y, width, height), angleStart, angleSweep);
            }
            
        }


        public override void DrawRoundRectangle(int x, int y, int width, int height, int radius, Boolean isFill)
        {
            Rectangle roundedRectangle = Rectangle.Round(new Rectangle(x,y,width,height));
            graphics.DrawLine(Pens.Black, x + radius, y, x + width - (radius), y);
            graphics.DrawArc(Pens.Black, x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90);
            graphics.DrawLine(Pens.Black, x + width, y + radius, x + width, y + height - (radius));
            graphics.DrawArc(Pens.Black, x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90);

            graphics.DrawLine(Pens.Black, x + width - (radius), y + height, x + radius, y + height); // Line
            graphics.DrawArc(Pens.Black, x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Corner
            graphics.DrawLine(Pens.Black, x, y + height - (radius), x, y + radius); // Line
            graphics.DrawArc(Pens.Black, x, y, radius * 2, radius * 2, 180, 90); // Corner
            
        }
    }
}
